<?php

class Content_controller extends CI_Controller {
    
    function Content_controller() {
        parent::__construct();
        
        $this->load->model('Content_model');
    }
    
    public function index() {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $this->load->model('Content_model');
        $this->load->model('Menu_model');
        $this->load->model('Language_model');

        $data['route'] = 'pages';
        $data['pages'] = $this->Content_model->contents_list();
        $data['options'] = $this->Menu_model->menu_leaf_list();
        $data['languages'] = $this->Language_model->languages_list();
        
        $this->load->view('layout/admin', $data);
    }
    
    public function set() {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $data = $this->input->post();
        $this->Content_model->content_set($data['parent'], $this->session->userdata('member_id'), $data['language'], $data['name'], $data['name'], $data['content']);
        
        $this->load->model('Content_model');
        $this->load->model('Menu_model');
        $this->load->model('Language_model');

        $data['route'] = 'view_pages';
        $data['pages'] = $this->Content_model->contents_language_list($data['language']);
        $data['language'] = $this->Language_model->language_get($data['language']);
        $data['languages'] = $this->Language_model->languages_select($data['language']);

        $this->load->view('layout/admin', $data);
    }
    
    public function delete($content_id) {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $content = $this->Content_model->content_get($content_id);
        $this->Content_model->content_delete($content_id);
        
        $this->load->model('Language_model');
        
        $data['route'] = 'view_pages';
        $data['pages'] = $this->Content_model->contents_language_list($content['language_id']);
        $data['language'] = $this->Language_model->language_get($content['language_id']);
        $data['languages'] = $this->Language_model->languages_select($content['language_id']);

        $this->load->view('layout/admin', $data);
    }
    
    public function view($menu_id, $language_id) {
        $content = $this->Content_model->content_menu_get($menu_id, $language_id);
        $data['content'] = $content['content'];
        
        $this->load->view('cms_pages/index_detail', $data);
    }
    
    public function update($content_id) {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $data = $this->input->post();
        $this->Content_model->content_update($content_id, $data['parent'], $data['language'], $data['name'], $data['name'], $data['content']);
        
        $this->load->model('Language_model');
        
        $data['route'] = 'view_pages';
        $data['pages'] = $this->Content_model->contents_language_list($data['language']['language_id']);
        $data['language'] = $this->Language_model->language_get($data['language']['language_id']);
        $data['languages'] = $this->Language_model->languages_select($data['language']['language_id']);

        $this->load->view('layout/admin', $data);
    }
    
    public function return_language_content($language_id) {
        $content = $this->Content_model->content_menu_get($menu_id, $language_id);
        $data['content'] = $content['content'];
        
        $this->load->view('cms_pages/index_detail', $data);
    }    
    
}

?>